//
//  MeetingContentView.m
//  YUNWUMeetingDemo
//
//  Created by mac on 2019/7/16.
//  Copyright © 2019 DGL. All rights reserved.
//

#import "MeetingContentView.h"
#import "CustomCameraView.h"

@interface MeetingContentView ()


@end

@implementation MeetingContentView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setViewUI];
        self.backgroundColor = UIColorFromRGBA(31, 47, 65, 1.0);
        
    }
    return self;
}

-(void)updateVideo:(UIView*)view data:(NSArray<UsrVideoId *> *)dataSource
{
    NSArray<UIView *>* subViews = [view subviews];
    for (int i = 0; i< [subViews count]; i++)
    {
        CustomCameraView* desView = (CustomCameraView*)subViews[i];
        UsrVideoId *videoID = i<[dataSource count]? dataSource[i]:nil;
        [desView setUsrVideoId:videoID];
    }
}

-(void)clearVideo:(UIView*)view
{
    NSArray<UIView *>* subViews = [view subviews];
    for (UIView* obj in subViews)
    {
        CustomCameraView* desView = (CustomCameraView*)obj;
        [desView setUsrVideoId:nil];
    }
}

- (void)updateUIViews:(NSArray<UsrVideoId *> *)dataSource localer:(NSString *)localer{
    NSUInteger count = [dataSource count];
    if (count == 1) {
        self.type = MContentViewTypeTwo;
        [self updateVideo:_twoView data:dataSource];
    }else if (count == 2) {
        self.type = MContentViewTypeTwo;
        [self updateVideo:_twoView data:dataSource];
    }else if (count == 3) {
        self.type = MContentViewTypeFour;
        [self updateVideo:_fourView data:dataSource];
    } else if (count == 4) {
        self.type = MContentViewTypeFour;
        [self updateVideo:_fourView data:dataSource];
    }
    else if (count == 5) {
        self.type = MContentViewTypeNine;
        [self updateVideo:_nineView data:dataSource];
    } else if (count == 6) {
        self.type = MContentViewTypeNine;
        [self updateVideo:_nineView data:dataSource];
    }
    else if (count == 7) {
        self.type = MContentViewTypeNine;
        [self updateVideo:_nineView data:dataSource];
    } else if (count == 8) {
        self.type = MContentViewTypeNine;
        [self updateVideo:_nineView data:dataSource];
    } else if (count >=9) {
        self.type = MContentViewTypeNine;
        [self updateVideo:_nineView data:dataSource];
    }
    else {
        [self clearVideo:_twoView];
        [self clearVideo:_fourView];
        [self clearVideo:_nineView];
    }
}

#pragma mark -- UI
- (void)setViewUI{
    _twoView = [[TwoVideoView alloc]init];
    _twoView.backgroundColor = [UIColor whiteColor];
    _twoView.alpha = 0;
    [self addSubview:_twoView];
    [_twoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left);
        make.top.mas_equalTo(self.mas_top);
        make.right.mas_equalTo(self.mas_right);
        make.bottom.mas_equalTo(self.mas_bottom);
    }];
    
    _fourView = [[FourVideoView alloc]init];
    _fourView.backgroundColor = [UIColor whiteColor];
    _fourView.alpha = 0;
    [self addSubview:_fourView];
    [_fourView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left);
        make.top.mas_equalTo(self.mas_top);
        make.right.mas_equalTo(self.mas_right);
        make.bottom.mas_equalTo(self.mas_bottom);
    }];
    
//    _sixView = [[SixVideoView alloc]init];
//    _sixView.backgroundColor = [UIColor whiteColor];
//    _sixView.alpha = 0;
//    [self addSubview:_sixView];
//    [_sixView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.mas_left);
//        make.top.mas_equalTo(self.mas_top);
//        make.right.mas_equalTo(self.mas_right);
//        make.bottom.mas_equalTo(self.mas_bottom);
//    }];
    
    _nineView = [[NineVideoView alloc]init];
    _nineView.backgroundColor = [UIColor whiteColor];
    _nineView.alpha = 0;
    [self addSubview:_nineView];
    [_nineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left);
        make.top.mas_equalTo(self.mas_top);
        make.right.mas_equalTo(self.mas_right);
        make.bottom.mas_equalTo(self.mas_bottom);
    }];
}

- (void)setType:(MContentViewType)type{
    _type = type;
    switch (type) {
        case MContentViewTypeTwo:{
            [UIView animateWithDuration:0.25 animations:^{
                _twoView.alpha = 1.0;
                _fourView.alpha = 0;
//                _sixView.alpha = 0;
                _nineView.alpha = 0;
            }];
            break;
        }
        case MContentViewTypeFour:{
            [UIView animateWithDuration:0.25 animations:^{
                _twoView.alpha = 0;
                _fourView.alpha = 1.0;
//                _sixView.alpha = 0;
                _nineView.alpha = 0;
            }];
            break;
        }
//        case MContentViewTypeSix:{
//            [UIView animateWithDuration:0.25 animations:^{
//                _twoView.alpha = 0;
//                _fourView.alpha = 0;
//                _sixView.alpha = 1.0;
//                _nineView.alpha = 0;
//            }];
//            break;
//        }
        case MContentViewTypeNine:{
            [UIView animateWithDuration:0.25 animations:^{
                //_twoView.alpha = 0;
                _fourView.alpha = 0;
//                _sixView.alpha = 0;
                _nineView.alpha = 1.0;
            }];
            break;
        }
    }
}


@end
